package com.rowline.admin.module.study.comparator;

import java.text.SimpleDateFormat;
import java.util.*;

import static java.util.Collections.sort;

/**
 * ClassName:Book
 * Created by 245167
 * on 2018/1/3  03:52
 * description:
 * compare（a,b）方法:根据第一个参数小于、等于或大于第二个参数分别返回负整数、零或正整数。
   equals（obj）方法：仅当指定的对象也是一个 Comparator，并且强行实施与此 Comparator 相同的排序时才返回 true。
   Collections.sort(list, new PriceComparator());的第二个参数返回一个int型的值，就相当于一个标志，告诉sort方法按什么顺序来对list进行排序。
 *
 */
public class Book implements Comparable {

    public int id;// 编号
    public String name;// 名称
    public double price; // 价格
    private String author;// 作者
    public GregorianCalendar calendar;// 出版日期

    public Book() {
        this.id = 0;
        this.name = "***";
        this.price = 0.0;
        this.author = "";
        this.calendar = new GregorianCalendar();
    }

    public Book(int id, String name, double price, GregorianCalendar calendar, String author) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.author = author;
        this.calendar = calendar;
    }


    @Override
    public int compareTo(Object o) {
        Book b = (Book) o;
        return this.id - b.id;
    }

    @Override
    public String toString() {
        SimpleDateFormat formatDate = new SimpleDateFormat("yyyy年MM月dd日");
        return "Book{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", price=" + price +
                ", author='" + author + '\'' +
                ", calendar=" + formatDate.format(calendar.getTime()) +
                '}';
    }


    public static void main(String[] args) {

        Book b1 = new Book(10000, "红楼梦", 150.86, new GregorianCalendar(2009,
                01, 25), "曹雪芹、高鄂");
        Book b2 = new Book(10003, "三国演义", 99.68, new GregorianCalendar(2008, 7,
                8), "罗贯中 ");
        Book b3 = new Book(10004, "水浒传", 100.8, new GregorianCalendar(2009, 6,
                28), "施耐庵 ");
        Book b4 = new Book(10001, "西游记", 120.8, new GregorianCalendar(2011, 6,
                8), "吴承恩");
        Book b5 = new Book(10002, "天龙八部", 10.4, new GregorianCalendar(2011, 9,
                23), "搜狐");

        TreeMap tm = new TreeMap();
        tm.put(b1, new Integer(255));
        tm.put(b2, new Integer(122));
        tm.put(b3, new Integer(688));
        tm.put(b4, new Integer(453));
        tm.put(b5, new Integer(40));
        Iterator it = tm.keySet().iterator();
        Object key = null, value = null;
        Book bb = null;
        while (it.hasNext()) {
            key = it.next();
            bb = (Book) key;
            value = tm.get(key);
            System.out.println(bb.toString() + "\t库存：" + value);
        }

        System.out.println("\n*********************************************\n");

        List<Book> books = new ArrayList<Book>();
        books.add(b1);
        books.add(b2);
        books.add(b3);
        books.add(b4);
        books.add(b5);

        sort(books);

        System.out.println("数组序列中的元素:");
        myprint(books);
        Collections.sort(books, new PriceComparator()); // 根据价格排序
        System.out.println("按书的价格排序:");
        myprint(books);


    }

    // 自定义比较器：按书的价格排序
    public static class PriceComparator implements Comparator {
        @Override
        public int compare(Object object1, Object object2) {// 实现接口中的方法
            Book p1 = (Book) object1; // 强制转换
            Book p2 = (Book) object2;
            return new Double(p1.price).compareTo(new Double(p2.price));
        }
    }

    // 自定义方法：分行打印输出list中的元素
    public static void myprint(List<Book> list) {
        Iterator it = list.iterator(); // 得到迭代器，用于遍历list中的所有元素
        while (it.hasNext()) {// 如果迭代器中有元素，则返回true
            System.out.println("\t" + it.next());// 显示该元素
        }
    }

}
